package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.MemberBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.MemberManagerAdapter;
import com.qiantu.phone.ui.dialog.BottomAddMemberDialog;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberManagerActivity extends AppActivity {
    List<MemberBean> members;
    List<MemberBean> admins;
    private MemberBean owner;
    private TextView ownerName;
    private View ownerLy;
    private RecyclerView otherHouseList;
    private MemberManagerAdapter memberAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_manager;
    }

    @Override
    protected void initView() {
        if (AppApplication.getInstance().getCurHouseInfo().getRoleType() != 0) {
            getTitleBar().getRightView().setVisibility(View.GONE);
        }
        ownerName = (TextView) findViewById(R.id.owner_name);
        otherHouseList = (RecyclerView) findViewById(R.id.other_house_list);
        ownerLy = findViewById(R.id.owner_ly);
        otherHouseList.setLayoutManager(new LinearLayoutManager(this));
        memberAdapter = new MemberManagerAdapter(getContext());
        otherHouseList.setAdapter(memberAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    private void refreshData(){
        members = new ArrayList<>();
        admins = new ArrayList<>();
        LLHttpManager.getHouseMembers(this, AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<List<MemberBean>>>(this) {
            @Override
            public void onSucceed(HttpData<List<MemberBean>> result) {
                for (MemberBean bean : result.getData()) {
                    if (bean.getRoleType() == 0) {
                        owner = bean;
                    } else if(bean.getRoleType() == 1){
                        admins.add(bean);
                    } else {
                        members.add(bean);
                    }
                }
                List<Map<String,Object>> datas = new ArrayList<>();
                Map<String,Object> data = new HashMap<>();
                data.put("name","管理员");
                data.put("data",admins);
                if(admins.size() != 0){
                    datas.add(data);
                }
                Map<String,Object> member = new HashMap<>();
                member.put("name","普通成员");
                member.put("data",members);
                if(members.size() != 0){
                    datas.add(member);
                }
                memberAdapter.setData(datas);
                if (owner!=null){
                    ownerLy.setVisibility(View.VISIBLE);
                    if (AppApplication.getInstance().getLoginInfo().getUserInfo().getUserSerialNo().equals(owner.getUserSerialNo())){
                        ownerName.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
                        ownerName.setText(owner.getUserNickName() + " (我)");
                    }else {
                        ownerName.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor6));
                        ownerName.setText(owner.getUserNickName());
                    }
                }else {
                    ownerLy.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onRightClick(View view) {
        showBottomEditDialog();
    }

    public void gotoMemberInfoView( MemberBean memberBean){
        MemberInfoActivity.start(MemberManagerActivity.this, memberBean);
    }

    private BottomAddMemberDialog mBottomEditDialog;
    private void showBottomEditDialog() {
        if (mBottomEditDialog == null) {
            mBottomEditDialog = new BottomAddMemberDialog(this);
            mBottomEditDialog.setDialogClickListener(new BottomAddMemberDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {
                    if(mBottomEditDialog.getRoleType().equals("1")){
                        createMember(mBottomEditDialog.getText(),mBottomEditDialog.getRoleType());
                    }else{
                        gotoRoomView(mBottomEditDialog.getText());
                    }
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomEditDialog);
        }
        mBottomEditDialog.setText1("");
        mBottomEditDialog.setText2("");
        mBottomEditDialog.show();
    }

    private void gotoRoomView(String[] text){
        Intent intent = new Intent(this,MemberRoomActivity.class);
        intent.putExtra("phone",text[0]);
        intent.putExtra("remark",text[1]);
        intent.putExtra("type",1);
        startActivity(intent);
    }

    private void createMember(String[] text,String roleType) {
        LLHttpManager.inviteMember(this, AppApplication.getInstance().getCurrentHouseSerialNo(), text[0], text[1],roleType, null, new HttpCallback<HttpData<MemberBean>>(this){
            @Override
            public void onSucceed(HttpData<MemberBean> result) {
                super.onSucceed(result);
                if (result.getData()!=null){
                    admins.add(result.getData());
                    memberAdapter.notifyDataSetChanged();
                }
            }
        });
    }

}